<?php
namespace classes;
class FileSystem{
	/**
	 * 创建目录 
	 */
	public function makeDirectory($path,$mode=0777,$recursive=true){
		if (file_exists($path)) {
			return false;
		}
		return mkdir($path,$mode,$recursive);
	}
	/**
	 *目录重命名 
	 */
	public function renameDirectory($oldname,$newname){		
		if(file_exists($oldname) && !file_exists($newname)){
			return rename($oldname, $newname);
		}
		return false;
	}
	/**
	 *移动目录 
	 */
	public function moveDirectory($source,$destination){
		return $this->renameDirectory($source,$destination);
	}
	/**
	 *删除目录 
	 */
	public function removeDirectory(){
		
	}
	/**
	 *返回目录是否为空 
	 */
	public function isEmptyDirectory($path){
		if (!file_exists($path)) {
			return false;
		}
		$handle = opendir($path);
		while(($item = readdir($handle)) !== false){
			if($item != '.' && $item != '..'){
				closedir($handle);
				return false;
			}
		}
		closedir($handle);
		return true;
	}
	/**
	 *复制目录 
	 */
	public function copyDirectory(){
		
	}
	
	/**
	 *获取文件信息 
	 */
	public function getFileInfo($filename){
		$fileInfo['name'] = basename($filename);
		$fileInfo['extension'] = $this->_getExtension($filename);
		$fileInfo['mtime'] = filemtime($filename); 
		$fileInfo['atime'] = fileatime($filename);  
		$fileInfo['ctime'] = filectime($filename);
		$fileInfo['size']  = $this->_toBytesUnit(filesize($filename));
		return $fileInfo;
	}
	
	protected function _getExtension($filename){
		return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
	}

	protected function _toBytesUnit($bytes){
		$units = array('B','KB','MB','GB','TB','PB','EB','ZB','YB','BB','NB','DB');
		$n = 0;
		while($bytes >= 1024){
			$bytes /=1024;
			$n++;
		}
		return round($bytes,2) . $units[$n];
	}
	/**
	 * 获取指定目录下的一级子目录及文件
	 * @param string $path,目录位置及名称
	 * @return array,由指定目录下的子目录名称和文件名称组成的数组
	 */
	public function getFiles($path){
		$handle = opendir($path);
		while(($item = readdir($handle)) !== false){
			if($item != '.' && $item != '..'){
				if(is_file($path . '/' . $item)){
					$array['files'][] = $item;
				}
				if(is_dir($path . '/' . $item)){
					$array['folders'][] = $item;
				}
			}
		}
		closedir($handle);
		return $array;
	}
}